using System;
using System.Data;
using System.Configuration;
using System.Web.UI.WebControls;
using System.IO;
public partial class ViewCCD : System.Web.UI.UserControl
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            HiddenFieldSortOrder.Value = "ASC";
            HiddenFieldSortExpression.Value = "Creation_Date_Time";
            BindGridView();
        }
    }

    private void BindGridView()
    {

        if (!string.IsNullOrEmpty(ControlHelperClass.CCDFolderPath))
        {
            DirectoryInfo dirInfo = new DirectoryInfo(ControlHelperClass.CCDFolderPath);
            FileInfo[] fileInfo = dirInfo.GetFiles("*.xml", SearchOption.AllDirectories);
            DataTable CCDTable = new DataTable();
            CCDTable.Columns.Add("Creation_Date_Time");
            CCDTable.Columns.Add("CCD_Document");
            CCDTable.Columns.Add("Upload");
            foreach (FileInfo finfo in fileInfo)
            {
                if (finfo.Name.Contains(ControlHelperClass.PatientId.ToString()))
                {
                    object[] parameter = new object[3];
                    parameter[0] = finfo.CreationTime;
                    parameter[1] = finfo.Name;
                    if (finfo.FullName.ToLower().Contains("uploaded"))
                    {
                        parameter[2] = "Y";
                    }
                    else
                    {
                        parameter[2] = "N";
                    }
                    CCDTable.Rows.Add(parameter);
                }
            }
            if (CCDTable.Rows.Count > 0)
            {
                CCDTable.DefaultView.Sort = HiddenFieldSortExpression.Value + " " + HiddenFieldSortOrder.Value;
                GridViewCCDDocuments.DataSource = CCDTable;
                GridViewCCDDocuments.DataBind();
                LabelNoRecords.Visible = false;
            }
            else
            {
                LabelNoRecords.Text = "No CCD documents found";
                LabelNoRecords.Visible = true;
            }

        }
    }

    public void Button1_Click(object o, EventArgs e)
    {
        LinkButton Button1 = (LinkButton)o;
        GridViewRow grdRow = (GridViewRow)Button1.Parent.Parent;
        LinkButton documentName = (LinkButton)grdRow.Cells[1].FindControl("LabelDocumentName");
        Label Uploaded = (Label)grdRow.Cells[2].FindControl("LabelUploadFlag");
        string fileName = documentName.Text;
        string ccdFilePath = string.Empty;
        if (Uploaded.Text.ToUpper().Equals("Y"))
        {
            ccdFilePath = Path.Combine(ControlHelperClass.CCDFolderPath + @"\Uploaded", fileName);
        }
        else
        {
            ccdFilePath = Path.Combine(ControlHelperClass.CCDFolderPath, fileName);
        }
        if (!String.IsNullOrEmpty(ConfigurationManager.AppSettings["CCDXslFilePath"].ToString()))
        {
            MultiViewCCD.ActiveViewIndex = 0;
            CCDXml.TransformSource = ConfigurationManager.AppSettings["CCDXslFilePath"].ToString();
            CCDXml.DocumentSource = ccdFilePath;
        }
        else
        {
            MultiViewCCD.ActiveViewIndex = 1;

            using (StreamReader ccdStream = new StreamReader(ccdFilePath, true))
            {
                DivCCDXml.InnerHtml = ControlHelperClass.HandleJunkChar(ccdStream.ReadToEnd());
            }

        }
    }

    protected void GridViewCCDDocuments_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridViewCCDDocuments.PageIndex = e.NewPageIndex;
        BindGridView();
    }
    protected void GridViewCCDDocuments_Sorting(object sender, GridViewSortEventArgs e)
    {
        HiddenFieldSortExpression.Value = e.SortExpression;
        if (HiddenFieldSortOrder.Value != "ASC")
        {
            HiddenFieldSortOrder.Value = "ASC";
        }
        else
        {
            HiddenFieldSortOrder.Value = "DESC";
        }
        BindGridView();
    }
}
